# Dreams have to come true after all

from datetime import datetime

from werkzeug.security import generate_password_hash, check_password_hash

from comment import db


# 用户的模型类
class User(db.Model):
    __tablename__ = 't_user'
    id = db.Column(db.BIGINT, primary_key=True, autoincrement=True)
    username = db.Column(db.String(64), doc='用户名')
    password = db.Column(db.String(128), doc='密码')
    payPassword = db.Column(db.String(128), doc='支付密码')
    email = db.Column(db.String(100), doc='邮箱')
    emailStatus = db.Column(db.Integer, doc='邮箱验证', default=0)  # 1代码正常
    inviteId = db.Column(db.Integer, doc='邀请码')
    ip = db.Column(db.String(128), doc='ip')
    phone = db.Column(db.String(11), doc='手机号')
    onlock = db.Column(db.Integer, doc='用户状态')  # 0代码正常
    phoneStatus = db.Column(db.Integer, doc='手机验证', default=1)  # 1代码正常
    realName = db.Column(db.String(64), doc='真实姓名')
    remark = db.Column(db.String(500), doc='备注')
    realNameStatus = db.Column(db.Integer, doc='实名认证', default=0)  # 代表已认证
    nick_name = db.Column(db.String(200), doc='昵称')
    avatar = db.Column(db.String(128), doc='头像')
    idNum = db.Column(db.String(64), doc='身份证号码')
    sumFriends = db.Column(db.Integer, doc='邀请数量统计')
    role = db.Column(db.Integer, doc='是否管理员', default=0)  # 0普通用户 1管理员

    loginTime = db.Column(db.DateTime, default=datetime.now(), doc='登录时间')
    registerTime = db.Column(db.DateTime, default=datetime.now(), doc='用户注册的时间')

    @property
    def pwd(self):
        return self.password

    @pwd.setter
    def pwd(self, x_password):
        """
        根据明文规定,转换成密文
        :param x_password:
        :return:
        """
        self.password = generate_password_hash(x_password)

    def check_password(self, x_password):
        """
        明文
        :param x_password:
        :return:
        """
        return check_password_hash(self.password, x_password)


from sqlalchemy import ForeignKey


# 用户账户的模型类
class Account(db.Model):
    __tablename__ = 't_account'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    # 外键连接
    userId = db.Column(db.BIGINT, ForeignKey('t_user.id'), doc='用户表主键')
    # 懒查询
    user = db.relationship('User', backref=db.backref('accountInfo', lazy=True, uselist=False))
    total = db.Column(db.Float(10, 2), doc='账户余额', default=0)
    balance = db.Column(db.Float(10, 2), doc='账户总余额', default=0)
    frozen = db.Column(db.Float(10, 2), doc='账户总计冻结总额', default=0)
    inverstmentW = db.Column(db.Float(10, 2), doc='总计待收本金', default=0)
    interestTotal = db.Column(db.Float(10, 2), doc='总计待收利息', default=0)
    addCapitalTotal = db.Column(db.Float(10, 2), doc='月投总额', default=0)
    recyclingInterest = db.Column(db.Float(10, 2), doc='月取总额', default=0)
    capitalTotal = db.Column(db.Float(10, 2), doc='月乘总额', default=0)
    inverstmentA = db.Column(db.Float(10, 2), doc='已投资总额', default=0)
    interestA = db.Column(db.Float(10, 2), doc='已赚取利息', default=0)
    uApplyExtractMoney = db.Column(db.Float(10, 2), doc='申请提现金额', default=0)
    discount = db.Column(db.Float(8, 2), doc='代金券', default=0)
